<?php
class Content extends Ecore_Db {
	var $cache;
	var $clear;
	public function __construct() {
		$this->cache = new Ecore_Cache();
		$this->clear = $this->cache->clear;
	}
	public function getAll($intOff, $intLimit, &$intTotal) {
		try {						
			$this->prepare('call contents_getall(?,?)');		
			$this->bindParam(1, $intOff, PDO::PARAM_INT, 11);
			$this->bindParam(2, $intLimit, PDO::PARAM_INT, 4);
			$arrResult 	= $this->fetchAll();
			$intTotal	= isset($arrResult[0]['foundRows']) ? $arrResult[0]['foundRows'] : 0;
			return isset($arrResult) ? $arrResult : null;
		} catch(Exception $e) {
			Ecore_Function::logDb('Backend::Content::getAll - ' .$e->getMessage());			
			return null;
		}
	}
	public function get($intContentId) {
		try {						
			$this->prepare('call contents_get(?)');		
			$this->bindParam(1, $intContentId, PDO::PARAM_INT, 11);
			$arrResult 	= $this->fetchAll();
			return isset($arrResult[0]) ? $arrResult[0] : null;
		} catch(Exception $e) {
			Ecore_Function::logDb('Backend::Content::get - ' .$e->getMessage());			
			return null;
		}
	}
	public function add($intAdminId, $intLang, $strPage, $strSection, $strName, $strContent, $strImage='') {
		try {
			$this->prepare('call contents_add(?,?,?,?,?,?,?)');
			$this->bindParam(1, $intAdminId, PDO::PARAM_INT, 11);
			$this->bindParam(2, $intLang, PDO::PARAM_INT, 4);
			$this->bindParam(3, $strPage, PDO::PARAM_STR, 50);
			$this->bindParam(4, $strSection, PDO::PARAM_STR, 50);
			$this->bindParam(5, $strName, PDO::PARAM_STR, 100);
			$this->bindParam(6, $strContent);
			$this->bindParam(7, $strImage, PDO::PARAM_STR, 150);
			$arrResult 	= $this->fetchAll();		
			$intError	= isset($arrResult[0]['errCode']) ? $arrResult[0]['errCode'] : 0;
			if($intError == 0) {				
				
			}
			return $intError;
		} catch(Exception $e) {
			Ecore_Function::logDb('Backend::Content::add - ' . $e->getMessage());
			return null;
		}
	}	
	public function update($intAdminId, $intLang, $intContentId, $strPage, $strSection, $strName, $strContent, $strImage='') {
		try {
			$this->prepare('call contents_update(?,?,?,?,?,?,?,?)');
			$this->bindParam(1, $intAdminId, PDO::PARAM_INT, 11);
			$this->bindParam(2, $intLang, PDO::PARAM_INT, 4);			
			$this->bindParam(3, $intContentId, PDO::PARAM_INT, 11);
			$this->bindParam(4, $strPage, PDO::PARAM_STR, 50);
			$this->bindParam(5, $strSection, PDO::PARAM_STR, 50);
			$this->bindParam(6, $strName, PDO::PARAM_STR, 100);
			$this->bindParam(7, $strContent);
			$this->bindParam(8, $strImage, PDO::PARAM_STR, 150);
			$arrResult 	= $this->fetchAll();		
			$intError	= isset($arrResult[0]['errCode']) ? $arrResult[0]['errCode'] : 0;
			if($intError == 0) {
				$strPage 		= isset($arrResult[0]['page']) ? $arrResult[0]['page'] : 0;				
				$strImageOld 	= isset($arrResult[0]['image']) ? $arrResult[0]['image'] : '';
				
				if($strImage != '' && $strImageOld != '') {		
					$this->deleteImg($strImageOld);
				}
			}
			return $intError;
		} catch(Exception $e) {
			Ecore_Function::logDb('Backend::Content::update - ' . $e->getMessage());
			return null;
		}
	}	
	public function deleteImg($intContentId, $strImg='') {
		if(file_exists(DIR_OTHER . $strImg))
			@unlink(DIR_OTHER . $strImg);
	}	
}